home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 October / maximum-cd-2009-10.iso / DiscContents / Firefox Setup 3.5.exe / nonlocalized / chrome / browser.jar / content / browser / preferences / privacy.js < prev    next >
Encoding:
JavaScript  |  2009-06-24  |  18.0 KB  |  531 lines

  1. /*
  2. //@line 42 "e:\builds\moz2_slave\win32_build\build\browser\components\preferences\privacy.js"
  3. */
  4.  
  5. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  6.  
  7. var gPrivacyPane = {
  8.  
  9.   /**
  10.    * Whether the use has selected the auto-start private browsing mode in the UI.
  11.    */
  12.   _autoStartPrivateBrowsing: false,
  13.  
  14.   /**
  15.    * Sets up the UI for the number of days of history to keep, and updates the
  16.    * label of the "Clear Now..." button.
  17.    */
  18.   init: function ()
  19.   {
  20.     this._updateHistoryDaysUI();
  21.     this._updateSanitizeSettingsButton();
  22.     this.initializeHistoryMode();
  23.     this.updateHistoryModePane();
  24.     this.updatePrivacyMicroControls();
  25.     this.initAutoStartPrivateBrowsingObserver();
  26.   },
  27.  
  28.   // HISTORY MODE
  29.  
  30.   /**
  31.    * The list of preferences which affect the initial history mode settings.
  32.    * If the auto start private browsing mode pref is active, the initial
  33.    * history mode would be set to "Don't remember anything".
  34.    * If all of these preferences have their default values, and the auto-start
  35.    * private browsing mode is not active, the initial history mode would be
  36.    * set to "Remember everything".
  37.    * Otherwise, the initial history mode would be set to "Custom".
  38.    *
  39.    * Extensions adding their own preferences can append their IDs to this array if needed.
  40.    */
  41.   prefsForDefault: [
  42.     "browser.history_expire_days",
  43.     "browser.history_expire_days_min",
  44.     "browser.download.manager.retention",
  45.     "browser.formfill.enable",
  46.     "network.cookie.cookieBehavior",
  47.     "network.cookie.lifetimePolicy",
  48.     "privacy.sanitize.sanitizeOnShutdown"
  49.   ],
  50.  
  51.   /**
  52.    * The list of control IDs which are dependent on the auto-start private
  53.    * browsing setting, such that in "Custom" mode they would be disabled if
  54.    * the auto-start private browsing checkbox is checked, and enabled otherwise.
  55.    *
  56.    * Extensions adding their own controls can append their IDs to this array if needed.
  57.    */
  58.   dependentControls: [
  59.     "rememberHistoryDays",
  60.     "rememberAfter",
  61.     "rememberDownloads",
  62.     "rememberForms",
  63.     "keepUntil",
  64.     "keepCookiesUntil",
  65.     "alwaysClear",
  66.     "clearDataSettings"
  67.   ],
  68.  
  69.   /**
  70.    * Check whether all the preferences values are set to their default values
  71.    *
  72.    * @param aPrefs an array of pref names to check for
  73.    * @returns boolean true if all of the prefs are set to their default values,
  74.    *                  false otherwise
  75.    */
  76.   _checkDefaultValues: function(aPrefs) {
  77.     for (let i = 0; i < aPrefs.length; ++i) {
  78.       let pref = document.getElementById(aPrefs[i]);
  79.       if (pref.value != pref.defaultValue)
  80.         return false;
  81.     }
  82.     return true;
  83.   },
  84.  
  85.   /**
  86.    * Initialize the history mode menulist based on the privacy preferences
  87.    */
  88.   initializeHistoryMode: function PPP_initializeHistoryMode()
  89.   {
  90.     let mode;
  91.     let getVal = function (aPref)
  92.       document.getElementById(aPref).value;
  93.  
  94.     if (this._checkDefaultValues(this.prefsForDefault)) {
  95.       if (getVal("browser.privatebrowsing.autostart"))
  96.         mode = "dontremember";
  97.       else 
  98.         mode = "remember";
  99.     }
  100.     else
  101.       mode = "custom";
  102.  
  103.     document.getElementById("historyMode").value = mode;
  104.   },
  105.  
  106.   /**
  107.    * Update the selected pane based on the history mode menulist
  108.    */
  109.   updateHistoryModePane: function PPP_updateHistoryModePane()
  110.   {
  111.     let selectedIndex = -1;
  112.     switch (document.getElementById("historyMode").value) {
  113.     case "remember":
  114.       selectedIndex = 0;
  115.       break;
  116.     case "dontremember":
  117.       selectedIndex = 1;
  118.       break;
  119.     case "custom":
  120.       selectedIndex = 2;
  121.       break;
  122.     }
  123.     document.getElementById("historyPane").selectedIndex = selectedIndex;
  124.   },
  125.  
  126.   /**
  127.    * Update the private browsing auto-start pref and the history mode
  128.    * micro-management prefs based on the history mode menulist
  129.    */
  130.   updateHistoryModePrefs: function PPP_updateHistoryModePrefs()
  131.   {
  132.     let pref = document.getElementById("browser.privatebrowsing.autostart");
  133.     switch (document.getElementById("historyMode").value) {
  134.     case "remember":
  135.       pref.value = false;
  136.  
  137.       // select the remember history option if needed
  138.       let rememberHistoryCheckbox = document.getElementById("rememberHistoryDays");
  139.       if (!rememberHistoryCheckbox.checked) {
  140.         rememberHistoryCheckbox.checked = true;
  141.         this.onchangeHistoryDaysCheck();
  142.       }
  143.  
  144.       // select the remember downloads option if needed
  145.       if (!document.getElementById("rememberDownloads").checked)
  146.         document.getElementById("browser.download.manager.retention").value = 2;
  147.  
  148.       // select the remember forms history option
  149.       document.getElementById("browser.formfill.enable").value = true;
  150.  
  151.       // select the accept cookies option
  152.       document.getElementById("network.cookie.cookieBehavior").value = 0;
  153.       // select the cookie lifetime policy option
  154.       document.getElementById("network.cookie.lifetimePolicy").value = 0;
  155.  
  156.       // select the clear on close option
  157.       document.getElementById("privacy.sanitize.sanitizeOnShutdown").value = false;
  158.       break;
  159.     case "dontremember":
  160.       pref.value = true;
  161.       break;
  162.     }
  163.   },
  164.  
  165.   /**
  166.    * Update the privacy micro-management controls based on the
  167.    * value of the private browsing auto-start checkbox.
  168.    */
  169.   updatePrivacyMicroControls: function PPP_updatePrivacyMicroControls()
  170.   {
  171.     if (document.getElementById("historyMode").value == "custom") {
  172.       let disabled = this._autoStartPrivateBrowsing =
  173.         document.getElementById("privateBrowsingAutoStart").checked;
  174.       this.dependentControls
  175.           .forEach(function (aElement)
  176.                    document.getElementById(aElement).disabled = disabled);
  177.  
  178.       // adjust the cookie controls status
  179.       this.readAcceptCookies();
  180.       document.getElementById("keepCookiesUntil").value = disabled ? 2 :
  181.         document.getElementById("network.cookie.lifetimePolicy").value;
  182.  
  183.       // adjust the checked state of the sanitizeOnShutdown checkbox
  184.       document.getElementById("alwaysClear").checked = disabled ? false :
  185.         document.getElementById("privacy.sanitize.sanitizeOnShutdown").value;
  186.  
  187.       // adjust the checked state of the remember history checkboxes
  188.       document.getElementById("rememberHistoryDays").checked = disabled ? false :
  189.         document.getElementById("browser.history_expire_days").value > 0;
  190.       this.onchangeHistoryDaysCheck();
  191.       document.getElementById("rememberDownloads").checked = disabled ? false :
  192.         this.readDownloadRetention();
  193.       document.getElementById("rememberForms").checked = disabled ? false :
  194.         document.getElementById("browser.formfill.enable").value;
  195.  
  196.       if (!disabled) {
  197.         // adjust the Settings button for sanitizeOnShutdown
  198.         this._updateSanitizeSettingsButton();
  199.       }
  200.     }
  201.   },
  202.  
  203.   // PRIVATE BROWSING
  204.  
  205.   /**
  206.    * Install the observer for the auto-start private browsing mode pref.
  207.    */
  208.   initAutoStartPrivateBrowsingObserver: function PPP_initAutoStartPrivateBrowsingObserver()
  209.   {
  210.     let prefService = document.getElementById("privacyPreferences")
  211.                               .service
  212.                               .QueryInterface(Components.interfaces.nsIPrefBranch2);
  213.     prefService.addObserver("browser.privatebrowsing.autostart",
  214.                             this.autoStartPrivateBrowsingObserver,
  215.                             true);
  216.   },
  217.  
  218.   autoStartPrivateBrowsingObserver:
  219.   {
  220.     QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIObserver,
  221.                                            Components.interfaces.nsISupportsWeakReference]),
  222.  
  223.     observe: function PPP_observe(aSubject, aTopic, aData)
  224.     {
  225.       let privateBrowsingService = Components.classes["@mozilla.org/privatebrowsing;1"].
  226.         getService(Components.interfaces.nsIPrivateBrowsingService);
  227.  
  228.       // Toggle the private browsing mode without switching the session
  229.       let prefValue = document.getElementById("browser.privatebrowsing.autostart").value;
  230.       let keepCurrentSession = document.getElementById("browser.privatebrowsing.keep_current_session");
  231.       keepCurrentSession.value = true;
  232.       // If activating from within the private browsing mode, reset the
  233.       // private session
  234.       if (prefValue && privateBrowsingService.privateBrowsingEnabled)
  235.         privateBrowsingService.privateBrowsingEnabled = false;
  236.       privateBrowsingService.privateBrowsingEnabled = prefValue;
  237.       keepCurrentSession.reset();
  238.     }
  239.   },
  240.  
  241.   // HISTORY
  242.  
  243.   /**
  244.    * Read the location bar enabled and suggestion prefs
  245.    * @return Int value for suggestion menulist
  246.    */
  247.   readSuggestionPref: function PPP_readSuggestionPref()
  248.   {
  249.     let getVal = function(aPref)
  250.       document.getElementById("browser.urlbar." + aPref).value;
  251.  
  252.     // Suggest nothing if autocomplete is not enabled
  253.     if (!getVal("autocomplete.enabled"))
  254.       return -1;
  255.  
  256.     // Bottom 2 bits of default.behavior specify history/bookmark
  257.     return getVal("default.behavior") & 3;
  258.   },
  259.  
  260.   /**
  261.    * Write the location bar enabled and suggestion prefs when necessary
  262.    * @return Bool value for enabled pref
  263.    */
  264.   writeSuggestionPref: function PPP_writeSuggestionPref()
  265.   {
  266.     let menuVal = document.getElementById("locationBarSuggestion").value;
  267.     let enabled = menuVal != -1;
  268.  
  269.     // Only update default.behavior if we're giving suggestions
  270.     if (enabled) {
  271.       // Put the selected menu item's value directly into the bottom 2 bits
  272.       let behavior = document.getElementById("browser.urlbar.default.behavior");
  273.       behavior.value = behavior.value >> 2 << 2 | menuVal;
  274.     }
  275.  
  276.     // Always update the enabled pref
  277.     return enabled;
  278.   },
  279.  
  280.   /*
  281.    * Preferences:
  282.    *
  283.    * NOTE: These first two are no longer shown in the UI. They're controlled
  284.    *       via the checkbox, which uses the zero state of the pref to turn
  285.    *       history off.
  286.    * browser.history_expire_days
  287.    * - the number of days of history to remember
  288.    * browser.history_expire_days.mirror
  289.    * - a preference whose value mirrors that of browser.history_expire_days, to
  290.    *   make the "days of history" checkbox easier to code
  291.    *
  292.    * browser.history_expire_days_min
  293.    * - the mininum number of days of history to remember
  294.    * browser.history_expire_days_min.mirror
  295.    * - a preference whose value mirrors that of browser.history_expire_days_min
  296.    *   to make the "days of history" checkbox easier to code
  297.    * browser.formfill.enable
  298.    * - true if entries in forms and the search bar should be saved, false
  299.    *   otherwise
  300.    * browser.download.manager.retention
  301.    * - determines when downloads are automatically removed from the download
  302.    *   manager:
  303.    *
  304.    *     0 means remove downloads when they finish downloading
  305.    *     1 means downloads will be removed when the browser quits
  306.    *     2 means never remove downloads
  307.    */
  308.  
  309.   /**
  310.    * Initializes the days-of-history mirror preference and connects it to the
  311.    * days-of-history checkbox so that updates to the textbox are transmitted to
  312.    * the real days-of-history preference.
  313.    */
  314.   _updateHistoryDaysUI: function ()
  315.   {
  316.     var pref = document.getElementById("browser.history_expire_days");
  317.     var mirror = document.getElementById("browser.history_expire_days.mirror");
  318.     var pref_min = document.getElementById("browser.history_expire_days_min");
  319.     var textbox = document.getElementById("historyDays");
  320.     var checkbox = document.getElementById("rememberHistoryDays");
  321.  
  322.     // handle mirror non-existence or mirror/pref unsync
  323.     if (mirror.value === null || mirror.value != pref.value || 
  324.         (mirror.value == pref.value && mirror.value == 0) )
  325.       mirror.value = pref.value ? pref.value : pref.defaultValue;
  326.  
  327.     checkbox.checked = (pref.value > 0);
  328.     textbox.disabled = !checkbox.checked;
  329.   },
  330.  
  331.   /**
  332.    * Responds to the checking or unchecking of the days-of-history UI, storing
  333.    * the appropriate value to the days-of-history preference and enabling or
  334.    * disabling the number textbox as appropriate.
  335.    */
  336.   onchangeHistoryDaysCheck: function ()
  337.   {
  338.     var pref = document.getElementById("browser.history_expire_days");
  339.     var mirror = document.getElementById("browser.history_expire_days.mirror");
  340.     var textbox = document.getElementById("historyDays");
  341.     var checkbox = document.getElementById("rememberHistoryDays");
  342.  
  343.     if (!this._autoStartPrivateBrowsing)
  344.       pref.value = checkbox.checked ? mirror.value : 0;
  345.     textbox.disabled = !checkbox.checked;
  346.   },
  347.  
  348.   /**
  349.    * Responds to changes in the days-of-history textbox,
  350.    * unchecking the history-enabled checkbox if the days
  351.    * value is zero.
  352.    */
  353.   onkeyupHistoryDaysText: function ()
  354.   {
  355.     var textbox = document.getElementById("historyDays");
  356.     var checkbox = document.getElementById("rememberHistoryDays");
  357.     
  358.     checkbox.checked = textbox.value != 0;
  359.   },
  360.  
  361.   /**
  362.    * Converts the value of the browser.download.manager.retention preference
  363.    * into a Boolean value.  "remove on close" and "don't remember" both map
  364.    * to an unchecked checkbox, while "remember" maps to a checked checkbox.
  365.    */
  366.   readDownloadRetention: function ()
  367.   {
  368.     var pref = document.getElementById("browser.download.manager.retention");
  369.     return (pref.value == 2);
  370.   },
  371.  
  372.   /**
  373.    * Returns the appropriate value of the browser.download.manager.retention
  374.    * preference for the current UI.
  375.    */
  376.   writeDownloadRetention: function ()
  377.   {
  378.     var checkbox = document.getElementById("rememberDownloads");
  379.     return checkbox.checked ? 2 : 0;
  380.   },
  381.  
  382.   // COOKIES
  383.  
  384.   /*
  385.    * Preferences:
  386.    *
  387.    * network.cookie.cookieBehavior
  388.    * - determines how the browser should handle cookies:
  389.    *     0   means enable all cookies
  390.    *     1   means reject third party cookies; see
  391.    *         netwerk/cookie/src/nsCookieService.cpp for a hairier definition
  392.    *     2   means disable all cookies
  393.    * network.cookie.lifetimePolicy
  394.    * - determines how long cookies are stored:
  395.    *     0   means keep cookies until they expire
  396.    *     1   means ask how long to keep each cookie
  397.    *     2   means keep cookies until the browser is closed
  398.    */
  399.  
  400.   /**
  401.    * Reads the network.cookie.cookieBehavior preference value and
  402.    * enables/disables the rest of the cookie UI accordingly, returning true
  403.    * if cookies are enabled.
  404.    */
  405.   readAcceptCookies: function ()
  406.   {
  407.     var pref = document.getElementById("network.cookie.cookieBehavior");
  408.     var acceptThirdParty = document.getElementById("acceptThirdParty");
  409.     var keepUntil = document.getElementById("keepUntil");
  410.     var menu = document.getElementById("keepCookiesUntil");
  411.  
  412.     // enable the rest of the UI for anything other than "disable all cookies"
  413.     var acceptCookies = (pref.value != 2);
  414.  
  415.     acceptThirdParty.disabled = !acceptCookies;
  416.     keepUntil.disabled = menu.disabled = this._autoStartPrivateBrowsing || !acceptCookies;
  417.     
  418.     return acceptCookies;
  419.   },
  420.  
  421.   readAcceptThirdPartyCookies: function ()
  422.   {
  423.     var pref = document.getElementById("network.cookie.cookieBehavior");
  424.     return pref.value == 0;
  425.   },
  426.  
  427.   /**
  428.    * Enables/disables the "keep until" label and menulist in response to the
  429.    * "accept cookies" checkbox being checked or unchecked.
  430.    */
  431.   writeAcceptCookies: function ()
  432.   {
  433.     var accept = document.getElementById("acceptCookies");
  434.     var acceptThirdParty = document.getElementById("acceptThirdParty");
  435.  
  436.     // if we're enabling cookies, automatically check 'accept third party'
  437.     if (accept.checked)
  438.       acceptThirdParty.checked = true;
  439.  
  440.     return accept.checked ? (acceptThirdParty.checked ? 0 : 1) : 2;
  441.   },
  442.  
  443.   writeAcceptThirdPartyCookies: function ()
  444.   {
  445.     var accept = document.getElementById("acceptCookies");
  446.     var acceptThirdParty = document.getElementById("acceptThirdParty");
  447.     return accept.checked ? (acceptThirdParty.checked ? 0 : 1) : 2;
  448.   },
  449.  
  450.   /**
  451.    * Displays fine-grained, per-site preferences for cookies.
  452.    */
  453.   showCookieExceptions: function ()
  454.   {
  455.     var bundlePreferences = document.getElementById("bundlePreferences");
  456.     var params = { blockVisible   : true, 
  457.                    sessionVisible : true, 
  458.                    allowVisible   : true, 
  459.                    prefilledHost  : "", 
  460.                    permissionType : "cookie",
  461.                    windowTitle    : bundlePreferences.getString("cookiepermissionstitle"),
  462.                    introText      : bundlePreferences.getString("cookiepermissionstext") };
  463.     document.documentElement.openWindow("Browser:Permissions",
  464.                                         "chrome://browser/content/preferences/permissions.xul",
  465.                                         "", params);
  466.   },
  467.  
  468.   /**
  469.    * Displays all the user's cookies in a dialog.
  470.    */  
  471.   showCookies: function (aCategory)
  472.   {
  473.     document.documentElement.openWindow("Browser:Cookies",
  474.                                         "chrome://browser/content/preferences/cookies.xul",
  475.                                         "", null);
  476.   },
  477.  
  478.   // CLEAR PRIVATE DATA
  479.  
  480.   /*
  481.    * Preferences:
  482.    *
  483.    * privacy.sanitize.sanitizeOnShutdown
  484.    * - true if the user's private data is cleared on startup according to the
  485.    *   Clear Private Data settings, false otherwise
  486.    */
  487.  
  488.   /**
  489.    * Displays the Clear Private Data settings dialog.
  490.    */
  491.   showClearPrivateDataSettings: function ()
  492.   {
  493.     document.documentElement.openSubDialog("chrome://browser/content/preferences/sanitize.xul",
  494.                                            "", null);
  495.   },
  496.  
  497.   /**
  498.    * Displays a dialog from which individual parts of private data may be
  499.    * cleared.
  500.    */
  501.   clearPrivateDataNow: function (aClearEverything)
  502.   {
  503.     var ts = document.getElementById("privacy.sanitize.timeSpan");
  504.     var timeSpanOrig = ts.value;
  505.     if (aClearEverything)
  506.       ts.value = 0;
  507.  
  508.     const Cc = Components.classes, Ci = Components.interfaces;
  509.     var glue = Cc["@mozilla.org/browser/browserglue;1"]
  510.                  .getService(Ci.nsIBrowserGlue);
  511.     glue.sanitize(window || null);
  512.  
  513.     // reset the timeSpan pref
  514.     if (aClearEverything)
  515.       ts.value = timeSpanOrig;
  516.   },
  517.  
  518.   /**
  519.    * Enables or disables the "Settings..." button depending
  520.    * on the privacy.sanitize.sanitizeOnShutdown preference value
  521.    */
  522.   _updateSanitizeSettingsButton: function ()
  523.    {
  524.     var settingsButton = document.getElementById("clearDataSettings");
  525.     var sanitizeOnShutdownPref = document.getElementById("privacy.sanitize.sanitizeOnShutdown");
  526.     
  527.     settingsButton.disabled = !sanitizeOnShutdownPref.value;      
  528.    }
  529.  
  530. };
  531.